package com.example.musicplayer;

import java.util.List;

import com.example.musicplayer.bean.Music;
import com.example.musicplayer.common.MusicConstant;
import com.example.musicplayer.service.MusicService;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MusicPlayActivity extends Activity implements OnClickListener {

    private static final String LOG_TAG = MusicPlayActivity.class.getSimpleName();

    private TextView mTextView;
    private Button mPlayBtn;
    private Button mStopBtn;
    private Button mPrevBtn;
    private Button mNextBtn;

    private Uri mMusicUri;
    private String mSongName;

    private List<Music> mMusics;
    private int mCurrentPosition = 0;

    private boolean mIsPlaying = false;

    private static final int MSG_START_PLAY = 1;
    private static final int MSG_PAUSE_PLAY = 2;
    private static final int MSG_STOP_PLAY = 3;
    private static final int MSG_PLAY_PREV = 4;
    private static final int MSG_PLAY_NEXT = 5;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case MSG_START_PLAY:
                startPlay();
                break;
            case MSG_PAUSE_PLAY:
                pausePlay();
                break;
            case MSG_STOP_PLAY:
                stopPlay();
                break;
            case MSG_PLAY_PREV:
                playPrev();
                break;
            case MSG_PLAY_NEXT:
                playNext();
                break;
            }
        };

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_music_play);

        initViews();

        initData();

        mHandler.sendEmptyMessage(MSG_START_PLAY);

        initEvent();
    }

    private void initViews() {
        mTextView = (TextView) findViewById(R.id.txt);

        mPlayBtn = (Button) findViewById(R.id.play);
        mStopBtn = (Button) findViewById(R.id.stop);
        mPrevBtn = (Button) findViewById(R.id.prev);
        mNextBtn = (Button) findViewById(R.id.next);
    }

    private void startPlay() {
        Intent intent = new Intent(getApplicationContext(), MusicService.class);
        intent.setAction(MusicService.ACTION_PLAY);
        if (mMusicUri != null) {
            intent.setData(mMusicUri);
        }
        intent.putExtra(MusicConstant.EXTRA_SONG_NAME, mSongName);
        startService(intent);

        mIsPlaying = true;
        updatePlayButtonState();
    }

    private void pausePlay() {
        Intent intent = new Intent(getApplicationContext(), MusicService.class);
        intent.setAction(MusicService.ACTION_PAUSE);
        startService(intent);

        mIsPlaying = false;
        updatePlayButtonState();
    }

    private void stopPlay() {
        Intent intent = new Intent(getApplicationContext(), MusicService.class);
        intent.setAction(MusicService.ACTION_STOP);
        startService(intent);

        mIsPlaying = false;
        updatePlayButtonState();
    }

    private void initData() {
        if (getIntent() != null) {
            Intent intent = getIntent();
            mMusicUri = intent.getData();
            if (intent.hasExtra(MusicConstant.EXTRA_SONG_NAME)) {
                mSongName = intent.getStringExtra(MusicConstant.EXTRA_SONG_NAME);
            }
            if (intent.hasExtra(MusicConstant.EXTRA_SONG_LIST)) {
                mMusics = intent.getParcelableArrayListExtra(MusicConstant.EXTRA_SONG_LIST);
            }
            mCurrentPosition = intent.getIntExtra(MusicConstant.EXTRA_SONG_POSITION, 0);
            if (mMusics != null) {
                playSong(mCurrentPosition);
            }
        }
    }

    private void initEvent() {
        mPlayBtn.setOnClickListener(this);
        mStopBtn.setOnClickListener(this);
        mPrevBtn.setOnClickListener(this);
        mNextBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.play:
            if (!mIsPlaying) {
                mHandler.sendEmptyMessage(MSG_START_PLAY);
            } else {
                mHandler.sendEmptyMessage(MSG_PAUSE_PLAY);
            }
            break;
        case R.id.stop:
            mHandler.sendEmptyMessage(MSG_STOP_PLAY);
            break;
        case R.id.prev:
            playPrev();
            break;
        case R.id.next:
            playNext();
            break;
        }
    }

    /**
     * This method must be called before mIsPlaying changed.
     */
    private void updatePlayButtonState() {
        if (mIsPlaying) {
            mPlayBtn.setText(R.string.pause);
        } else {
            mPlayBtn.setText(R.string.play);
        }
    }

    private void playPrev() {
        if (mMusics != null && mMusics.size() > 0) {
            if (mCurrentPosition > 0) {
                mCurrentPosition--;
            }
            playSong(mCurrentPosition);
        } else {
            Toast.makeText(MusicPlayActivity.this, "No Music found", Toast.LENGTH_SHORT).show();
        }
    }

    private void playSong(int position) {
        Music music = mMusics.get(position);

        mMusicUri = Uri.withAppendedPath(MusicConstant.MEDIA_EXTERNAL_CONTENT_URI,
              String.valueOf(music.getId()));

        mSongName = music.getTitle();

        mHandler.sendEmptyMessage(MSG_START_PLAY);

        mCurrentPosition = position;
    }

    private void playNext() {
        if (mMusics != null && mMusics.size() > 0) {
            if (mCurrentPosition < mMusics.size() - 1) {
                mCurrentPosition++;
            }
            playSong(mCurrentPosition);
        } else {
            Toast.makeText(MusicPlayActivity.this, "No Music found", Toast.LENGTH_SHORT).show();
        }
    }
}